package com.ztgf.shopping.api.cms.template;


import com.alibaba.fastjson.JSON;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.shopping.models.bo.template.TempletInfoSaveBO;
import com.ztgf.shopping.models.bo.template.TempletModuleBO;
import com.ztgf.shopping.models.dto.template.TempletInfoDTO;
import com.ztgf.shopping.models.dto.template.TempletModuleDTO;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.template.ITempletInfoService;
import com.ztgf.shopping.models.vo.template.TemplateInfoDVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 模版  前端控制器
 * </p>
 *
 * @author YUE
 * @since 2020-05-23
 */
@RestController
@RequestMapping("/cms/templetInfo")
@Slf4j
@Api(value = "TempletInfoController", tags = "模版表接口")
public class TempletInfoController {

    @DubboReference
    private ITempletInfoService iTempletInfoService;


    /**
     * 获取所有的平台首页模板
     *
     * @return
     */
    @ApiOperation(value = "所有平台 首页模板", notes = "所有平台 首页模板")
    @PostMapping(value = "/getListPlatformTemHome")
    public Result getListPlatformTemHome() {
        List<TemplateInfoDVO> listPlatformTemHome = iTempletInfoService.getListPlatformTemByType(ShoppingCodeEnum.TEMPLATE_TYPE_HOME.getCode());
        return new Result<>(listPlatformTemHome);
    }

    /**
     * 获取所有的平台活动页模板
     *
     * @return
     */
    @ApiOperation(value = "所有平台 活动页模板", notes = "所有平台 活动页模板")
    @PostMapping(value = "/getListPlatformTemActive")
    public Result getListPlatformTemActive() {
        List<TemplateInfoDVO> listPlatformTemHome = iTempletInfoService.getListPlatformTemByType(ShoppingCodeEnum.TEMPLATE_TYPE_ACTIVE.getCode());
        return new Result<>(listPlatformTemHome);
    }

    /**
     * 平台装修
     *
     * @param templetModuleBOListStr
     * @return
     */
    @ApiOperation(value = "平台装修", notes = "平台装修")
    @PostMapping(value = "/issuePlatformTemplate")
    public Result issuePlatformTemplate(String templetModuleBOListStr) {
        List<TempletModuleBO> templetModuleBOList = JSON.parseArray(templetModuleBOListStr, TempletModuleBO.class);
        return iTempletInfoService.issuePlatformTemplate(templetModuleBOList);
    }

    /**
     * 平台装修
     *
     * @param templetModuleBOListStr
     * @return
     */
    @ApiOperation(value = "平台装修", notes = "平台装修")
    @PostMapping(value = "/savePlatformHome")
    public Result savePlatformHome(String templetModuleBOListStr) {
        List<TempletModuleBO> templetModuleBOList = JSON.parseArray(templetModuleBOListStr, TempletModuleBO.class);
        return iTempletInfoService.savePlatformHome(templetModuleBOList);
    }

    /**
     * 获取所有的平台金融页模板
     *
     * @return
     */
    @ApiOperation(value = "所有平台 活动页模板", notes = "所有平台 活动页模板")
    @PostMapping(value = "/queryPlatformFinancialList")
    public Result queryPlatformFinancialList() {
        List<TemplateInfoDVO> listPlatformTemHome = iTempletInfoService.getListPlatformTemByType(ShoppingCodeEnum.TEMPLATE_TYPE_FINANCIAL.getCode());
        return new Result<>(listPlatformTemHome);
    }

    /**
     * 编辑平台金融装修
     *
     * @param templetModuleBOListStr
     * @return
     */
    @ApiOperation(value = "编辑平台金融装修", notes = "编辑平台金融装修")
    @PostMapping(value = "/editPlatformFinancial")
    public Result editPlatformFinancial(String templetModuleBOListStr) {
        List<TempletModuleBO> templetModuleBOList = JSON.parseArray(templetModuleBOListStr, TempletModuleBO.class);
        return iTempletInfoService.editPlatformFinancial(templetModuleBOList);
    }

    /**
     * 新增平台金融装修
     *
     * @param templetModuleBOListStr
     * @return
     */
    @ApiOperation(value = "新增平台金融装修", notes = "新增平台金融装修")
    @PostMapping(value = "/insertPlatformFinancial")
    public Result insertPlatformFinancial(String templetModuleBOListStr, String name, String skipUrl) {
        List<TempletModuleBO> templetModuleBOList = JSON.parseArray(templetModuleBOListStr, TempletModuleBO.class);
        return iTempletInfoService.insertPlatformFinancial(templetModuleBOList, name, skipUrl);
    }

    /**
     * 删除平台金融装修
     *
     * @param templetId
     * @return
     */
    @ApiOperation(value = "删除平台金融装修", notes = "删除平台金融装修")
    @PostMapping(value = "/delPlatformFinancial")
    public Result delPlatformFinancial(Long templetId) {
        return iTempletInfoService.delPlatformFinancial(templetId);
    }


    /**
     * 删除平台金融装修
     *
     * @param templetId
     * @return
     */
    @ApiOperation(value = "设置默认平台金融装修", notes = "设置默认平台金融装修")
    @PostMapping(value = "/savePlatformFinancialDefault")
    public Result savePlatformFinancialDefault(Long templetId) {
        return iTempletInfoService.savePlatformFinancialDefault(templetId);
    }


    /**
     * 活动页装修（编辑）
     *
     * @param templetModuleBOListStr
     * @param name                   活动页名称
     * @return
     */
    @ApiOperation(value = "活动页装修（编辑）", notes = "活动页装修（编辑）")
    @PostMapping(value = "/issueActiveTemplate")
    public Result issueActiveTemplate(String templetModuleBOListStr, String name) {
        List<TempletModuleBO> templetModuleBOList = JSON.parseArray(templetModuleBOListStr, TempletModuleBO.class);
        return iTempletInfoService.issueActiveTemplate(templetModuleBOList, name);
    }

    /**
     * 活动页发布
     *
     * @param templetModuleBOListStr
     * @return
     */
    @ApiOperation(value = "活动页发布", notes = "活动页发布")
    @PostMapping(value = "/issueActiveCrTemplate")
    public Result issueActiveCrTemplate(String templetModuleBOListStr, String name, String skipUrl) {
        List<TempletModuleBO> templetModuleBOList = JSON.parseArray(templetModuleBOListStr, TempletModuleBO.class);
        return iTempletInfoService.createActiveTemplate(templetModuleBOList, name, skipUrl);
    }

    /**
     * 根据模板id获取下面的已有装修模板
     *
     * @param templateId 模板id
     * @return
     */
    @ApiOperation(value = "根据模板id获取下面的已有装修模板", notes = "根据模板id获取下面的已有装修模板")
    @PostMapping(value = "/getTemplateDecorationByTemplateId")
    @ApiImplicitParam(name = "templateId", value = "模版表id", required = true, paramType = "query", dataType = "Long")
    public Result getTemplateDecorationByTemplateId(Long templateId) {
        return iTempletInfoService.getTemplateDecorationByTemplateId(templateId);
    }

    /**
     * 获取模板id获取所有的功能(用于模板装修查看)
     *
     * @return
     */
    @ApiOperation(value = "获取模板id获取所有的功能", notes = "获取模板id获取所有的功能")
    @PostMapping(value = "/getListTemplateModuleByTemplateId")
    public Result<List<TempletModuleDTO>> getListTemplateModuleByTemplateId(Long templateId) {
        List<TempletModuleDTO> templetModuleDTOList = iTempletInfoService.getListTemplateModuleById(templateId);
        return new Result<>(templetModuleDTOList);
    }

//    /**
//     * 根据模板id获取平台模板
//     * @return
//     */
//    @ApiOperation(value = "平台 模板",notes = "平台 模板")
//    @PostMapping(value = "/getPlatformTemplateInfoByTemId")
//    public Result getPlatformTemplateInfoByTemId(Long templateId){
//        if(null == templateId){
//            return new Result<>(CodeEnum.FAIL_100002 );
//        }
//        TempletInfoVO templetInfoVO = iTempletInfoService.getPlatformInfoByTemId(templateId);
//        return new Result<>(templetInfoVO);
//    }

//    /**
//     * 根据模板id获取平台活动页模板
//     * @return
//     */
//    @ApiOperation(value = "平台活动页 模板",notes = "活动页 模板")
//    @PostMapping(value = "/getPlatformActiveTemplateInfo")
//    public Result getPlatformActiveTemplateInfo(Long templateId){
//        if(null == templateId){
//            return new Result<>(CodeEnum.FAIL_100002 );
//        }
//        TempletInfoVO templetInfoVO = iTempletInfoService.getPActiveInfoByTemId(templateId);
//        return new Result<>(templetInfoVO);
//    }
//
//    @ApiOperation(value = "添加模版",notes = "添加模版")
//    @PostMapping(value = "/insert")
//    public Result insert( TempletInfoSaveBO templetInfoSaveBO) {
//        boolean flag = iTempletInfoService.add(templetInfoSaveBO);
//        if (flag){
//            return new Result<>();
//        } else {
//            return new Result<>(CodeEnum.FAIL_100007);
//        }
//    }

//    @PostMapping("/save")
//    @ApiOperation(value = "保存模版",notes = "保存模版")
//    public Result save( TempletInfoSaveBO templetInfoSaveBO){
//        if(templetInfoSaveBO.getId() == null){
//            //添加
//            return  insert(templetInfoSaveBO);
//        } else {
//            return  update(templetInfoSaveBO);
//        }
//    }

    @ApiOperation(value = "修改模版", notes = "修改模版")
    @PostMapping("/update")
    public Result update(TempletInfoSaveBO templetInfoSaveBO) {
        TempletInfoDTO templetInfoDTO = new TempletInfoDTO();
        BeanUtils.copyProperties(templetInfoSaveBO, templetInfoDTO);
        templetInfoDTO.setId(templetInfoSaveBO.getId());
        boolean flag = iTempletInfoService.updateById(templetInfoDTO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除平台活动页模版", notes = "删除平台活动页模版")
    @ApiImplicitParam(name = "id", value = "模版表id", required = true, paramType = "query", dataType = "Long")
    @PostMapping("/deletePlateActive")
    public Result deletePlateActive(@RequestParam("id") Long id) {
        boolean flag = iTempletInfoService.deletePlateActive(id);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

//    @PostMapping("/deleteBatch")
//    @ApiOperation(value = "批量删除模版",notes = "批量删除模版")
//    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
//    public Result deleteBatch(@RequestParam("ids") String ids){
//        if(StrUtils.isEmpty(ids)){
//            return new Result(CodeEnum.FAIL_100002 );
//        }
//        String[] arr = ids.split(",");
//        iTempletInfoService.deleteBatchIds(Arrays.asList(arr));
//        return new Result<>();
//
//    }

//    @PostMapping("/get")
//    @ApiOperation(value = "根据id查找",notes = "根据id查找")
//    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
//    public Result<TempletInfoDTO> get(@RequestParam("id") Long id){
//        if(id == null){
//            return new Result<>(CodeEnum.FAIL_100002 );
//        }
//        TempletInfoDTO epgMainNavigationDTO = iTempletInfoService.selectById(id);
//        return new Result<>(epgMainNavigationDTO);
//    }
//
//    @ApiOperation(value = "模版列表",notes = "模版列表")
//    @PostMapping("/queryPageList")
//    public Result<Page<TempletInfoDTO>> queryPageList( TempletInfoBO templetInfoBO){
//        //Wrapper<EpgMainNavigationDTO> wrapper = new EntityWrapper<>();
//        Page<TempletInfoDTO> page = new Page<>(templetInfoBO.getPageNo(), templetInfoBO.getPageSize());
//        page = iTempletInfoService.selectPage(page);
//        return new Result<>(page);
//    }

    @PostMapping("/deleteActivityPageById")
    @ApiOperation(value = "删除活动页模板", notes = "删除活动页模板")
    @ApiImplicitParam(name = "id", value = "模版表id", required = true, paramType = "query", dataType = "Long")
    public Result deleteActivityPageById(Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return iTempletInfoService.deleteActivityPageById(id);
    }
}

